<?php

class Application_Model_PaczkaMapper
{

    protected $_dbTable;

    public function init()
    {
        $this->_dbTable = new Application_Model_DbTable_Paczka();
    }

    public function setDbTable($dbTable)
    {
        $dbTable = new $dbTable();

        if (!$dbTable instanceof Zend_Db_Table_Abstract)
        {
            throw new Exception('Invalid table data gateway provided');
        }

        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null == $this->_dbTable)
        {
            $this->setDbTable('Application_Model_DbTable_Paczka');
        }
        return $this->_dbTable;
    }

    public function nowaPaczka(Application_Model_Paczka $paczka)
    {
        $data = array(
            'nazwa' => $paczka->getNazwa()
        );
        $this->getDbTable()->insert($data);
    }

    public function usun($id)
    {
        $where = $this->getDbTable()->getAdapter()->quoteInto('id_rodz_pacz = ?', $id);
        $this->getDbTable()->delete($where);
    }

    public function podajPaczki()
    {
        $resultSet = $this->getDbTable()->fetchAll();

        $paczki = array();

        foreach ($resultSet as $row)
        {
            $paczka = new Application_Model_Paczka();

            $paczka->setIdRodzPacz($row->id_rodz_pacz)
                    ->setNazwa($row->nazwa);

            $paczki[] = $paczka;
        }
        return $paczki;
    }

    public function podajPaczka($id, Application_Model_Paczka $paczka)
    {
        $resultSet = $this->getDbTable()->find($id);
        $row = $resultSet->current();

        $paczka->setIdRodzPacz($row['id_rodz_pacz']);
        $paczka->setNazwa($row['nazwa']);
    }

    public function listaPaczek()
    {
        $resultSet = $this->getDbTable()->fetchAll();

        $paczki = array();

        foreach ($resultSet as $row)
        {
            $paczka = new Application_Model_Kierunek();

            $paczka->setKierunek($row->kierunek)            ;

            $paczki[] = $paczka;
        }
        return $paczki;
    }

}

